/*
 Trabajo Practico 3 Ejercicio 4
 Entrega número: 1

 - Integrantes:
	- Martinez, Guillermo	DNI: 34510461
	- Barrera, Lourdes		DNI: 33306634
	- Reyes, Leandro		DNI: 34213699
	- Rodriguez, Lucas		DNI: 34265017
*/
#include "defines.h"

void timestamp(char *arg)
{
	time_t t_val;
	struct tm * time_st;

	t_val = time(NULL);
	time_st = localtime(&t_val);

	// Copia time_st al buffer timestamp
	arg[0] = '\0';
	sprintf(arg,"%02d/%02d/%d %02d:%02d:%02d",time_st->tm_mday,time_st->tm_mon+1,
									time_st->tm_year+1900,time_st->tm_hour,
									time_st->tm_min, time_st->tm_sec);
}

void resta_timestamp(char *arg1, char *arg2)
{
	struct tm t1, t2;
	char aux[70];
	sscanf(arg1, "%d/%d/%d %d:%d:%d",&t1.tm_mday, &t1.tm_mon,
									 &t1.tm_year, &t1.tm_hour,
									 &t1.tm_min, &t1.tm_sec);

	sscanf(arg2, "%d/%d/%d %d:%d:%d",&t2.tm_mday, &t2.tm_mon,
									 &t2.tm_year, &t2.tm_hour,
									 &t2.tm_min, &t2.tm_sec);

	t1.tm_hour -= t2.tm_hour;
	t1.tm_min  -= t2.tm_min;
	t1.tm_sec  -= t2.tm_sec;
	t1.tm_mday -= t2.tm_mday;
	t1.tm_mon  -= t1.tm_mon;
	t1.tm_year -= t1.tm_year;

	if(t1.tm_sec < 0)
	{
		t1.tm_sec += 60;
		t1.tm_min--;
	}
	
	if(t1.tm_min < 0)
	{
		t1.tm_min += 60;
		t1.tm_hour--;
	}
	
	sprintf(arg2,"\nTiempo Resultante: ");
	
	if(t1.tm_mday > 0)
	{
		sprintf(aux,"%02d días ",t1.tm_mday);
		strcat(arg2,aux);
	}
	
	if(t1.tm_mon > 0)
	{
		sprintf(aux,"%02d meses ",t1.tm_mon);
		strcat(arg2,aux);
	}
	
	if(t1.tm_year > 0)
	{
		sprintf(aux,"%d años ",t1.tm_year);
		strcat(arg2,aux);
	}
	
	if(t1.tm_hour > 0)
	{
		sprintf(aux,"%02d horas ",t1.tm_hour);
		strcat(arg2,aux);
	}
	
	if(t1.tm_min > 0)
	{
		sprintf(aux,"%02d minutos ",t1.tm_min);
		strcat(arg2,aux);
	}
	
	sprintf(aux,"%02d segundos ",t1.tm_sec);
	strcat(arg2,aux);

    strcat(arg1, arg2);
}
